let db;
const request = indexedDB.open('myDatabase', 1);

request.onerror = function (event) {
    console.log("数据库打开失败");
};

request.onsuccess = function (event) {
    db = event.target.result;
    console.log("数据库打开成功");
};

request.onupgradeneeded = function (event) {
    // 数据库首次创建或版本升级时触发
    const db = event.target.result;

    // 创建一个对象存储（表）
    const objectStore = db.createObjectStore('customers', {
        keyPath: 'fileHash',
        autoIncrement: false
    });
};


export function addCustomer(customer) {
    const transaction = db.transaction(['customers'], 'readwrite');
    const objectStore = transaction.objectStore('customers');

    const request = objectStore.add(customer);

    request.onsuccess = function (event) {
        console.log('数据添加成功');
    };

    request.onerror = function (event) {
        console.log('数据添加失败');
    };
}

export function getCustomer(id) {
    const transaction = db.transaction(['customers']);
    const objectStore = transaction.objectStore('customers');

    const request = objectStore.get(id);

    request.onsuccess = function (event) {
        const customer = event.target.result;
    };

    request.onerror = function (event) {
        console.log('读取数据失败');
    };
}

export function deleteCustomer(id) {
    const transaction = db.transaction(['customers'], 'readwrite');
    const objectStore = transaction.objectStore('customers');
    
    const request = objectStore.delete(id);
    
    request.onsuccess = function(event) {
      console.log('数据删除成功');
    };
    
    request.onerror = function(event) {
      console.log('数据删除失败');
    };
  }
  